﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PraxisRent.Entities;
using PraxisRent.BL;
using Microsoft.Practices.Unity;

namespace PraxisRent.UI
{
    public partial class FrmMttoObra : Form
    {
        [Dependency]
        public IObraBL obraBL { get; set; }

        public FrmMttoObra()
        {
            InitializeComponent();
            this.BuilUp();
        }

        bool isNew;

        public void NuevaObra()
        {
            isNew = true;

            oBRABindingSource.AddNew();
        }

        public OBRA _obra { get; set; }

        public void EditarObra()
        {
            isNew = false;

            oBRABindingSource.DataSource = _obra;
        }

        private void FrmMttoObra_Load(object sender, EventArgs e)
        {

        }

        private void BtnGuardar_Click(object sender, EventArgs e)
        {
            var obra = oBRABindingSource.Current as OBRA;

            var msg = "ERROR";

            if (obra != null)
            {
                if (isNew == true)
                {
                    try
                    {
                        obra.Empresa = empresaTextBox.Text.ToUpper();
                        obra.Nombre = nombreTextBox.Text.ToUpper();
                        obra.Ubicacion = ubicacionTextBox.Text.ToUpper();
                        obraBL.AddObra(obra);
                        MessageBox.Show("Registro Correcto", "Registro",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                if (isNew == false)
                {
                    try
                    {
                        obra.Empresa = empresaTextBox.Text.ToUpper();
                        obra.Nombre = nombreTextBox.Text.ToUpper();
                        obra.Ubicacion = ubicacionTextBox.Text.ToUpper();
                        obraBL.UpdateObra(obra);
                        MessageBox.Show("Modificación Correcta", "Registro",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

        private void nombreTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            var amberson = Convert.ToString("&");
            var par1 = Convert.ToString("(");
            var par2 = Convert.ToString(")");
            var punto = Convert.ToString(".");
            var guion = Convert.ToString("-");
            var coma = Convert.ToString(",");


            string cadena = amberson + par1 + par2 + punto + guion + coma;

            if (Char.IsLetter(e.KeyChar) || cadena.Contains(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void ubicacionTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            var numeral = Convert.ToString("#");
            var asterisco = Convert.ToString("*");
            var guion = Convert.ToString("-");
            var par1 = Convert.ToString("(");
            var par2 = Convert.ToString(")");
            var nro = Convert.ToString("°");

            string cadena = numeral + asterisco + guion + par1 + par2 + nro;

            if (Char.IsLetter(e.KeyChar) || Char.IsNumber(e.KeyChar) || cadena.Contains(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void empresaTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            var amberson = Convert.ToString("&");
            var par1 = Convert.ToString("(");
            var par2 = Convert.ToString(")");
            var punto = Convert.ToString(".");
            var guion = Convert.ToString("-");
            var coma = Convert.ToString(",");


            string cadena = amberson + par1 + par2 + punto + guion + coma;

            if (Char.IsLetter(e.KeyChar) || cadena.Contains(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void telefeonoTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            var numeral = Convert.ToString("#");
            var asterisco = Convert.ToString("*");
            var guion = Convert.ToString("-");
            var par1 = Convert.ToString("(");
            var par2 = Convert.ToString(")");

            string cadena = numeral + asterisco + guion + par1 + par2 + (char)13;

            if (Char.IsNumber(e.KeyChar) || cadena.Contains(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void faxTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            var guion = Convert.ToString("-");
            var par1 = Convert.ToString("(");
            var par2 = Convert.ToString(")");

            string cadena = guion + par1 + par2 + (char)13;

            if (Char.IsNumber(e.KeyChar) || cadena.Contains(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }


    }
}
